package com.wms.web.controller.stock;

import com.wms.base.service.BaseMatClassService;
import com.wms.base.service.BaseMatGroupService;
import com.wms.base.service.BaseWarehouseService;
import com.wms.common.annotation.Log;
import com.wms.common.bean.request.MatLabelRequestData;
import com.wms.common.bean.request.PutOffRequestBody;
import com.wms.common.bean.request.PutOnRequestBody;
import com.wms.common.core.controller.BaseController;
import com.wms.common.core.domain.AjaxResult;
import com.wms.common.core.page.TableDataInfo;
import com.wms.common.enums.BusinessType;
import com.wms.common.utils.bean.BeanUtils;
import com.wms.common.utils.poi.ExcelUtils;
import com.wms.stock.domain.StockInfo;
import com.wms.stock.domain.StockMatLabel;
import com.wms.stock.domain.stats.StockInfoSummary;
import com.wms.stock.service.StockInfoService;
import com.wms.stock.service.StockMatLabelService;
import com.wms.system.service.SysDictDataService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 库存信息Controller
 */
@RestController
@RequestMapping("/stock/info")
public class StockInfoController extends BaseController {
    @Autowired
    private StockInfoService stockInfoService;
    @Autowired
    private BaseMatGroupService baseMatGroupService;
    @Autowired
    private BaseMatClassService baseMatClassService;
    @Autowired
    private BaseWarehouseService baseWarehouseService;
    @Autowired
    private SysDictDataService sysDictDataService;
    @Autowired
    private StockMatLabelService stockMatLabelService;

    private static final String dictType = "base_mat_unit";

    /**
     * 查询库存汇总
     *
     * @param stockInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('stock:record:list')")
    @GetMapping("/statsList")
    public TableDataInfo statsList(StockInfo stockInfo) {
        startPage();
        List<StockInfo> list = stockInfoService.selectStockInfoStatsList(stockInfo);
        if (CollectionUtils.isNotEmpty(list)) {
            for (StockInfo info : list) {
                info.setMatGroupName(baseMatGroupService.selectBaseMatGroupNameByGroupCode(info.getMatGroup()));
                info.setMatClassName(baseMatClassService.selectBaseMatClassNameByClassCode(info.getMatClass()));
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出库存汇总列表
     *
     * @param response
     * @param stockInfo
     */
    @PreAuthorize("@ss.hasPermi('stock:info:export')")
    @Log(title = "库存汇总", businessType = BusinessType.EXPORT)
    @PostMapping("/statsExport")
    public void statsExport(HttpServletResponse response, StockInfo stockInfo) {
        List<StockInfo> list = stockInfoService.selectStockInfoStatsList(stockInfo);
        ExcelUtils<StockInfoSummary> util = new ExcelUtils<StockInfoSummary>(StockInfoSummary.class);
        List<StockInfoSummary> summaryList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list)) {
            StockInfoSummary summary = null;
            for (StockInfo info : list) {
                summary = new StockInfoSummary();
                BeanUtils.copyBeanProp(summary, info);
                summaryList.add(summary);
            }
        }
        util.exportExcel(response, summaryList, "库存汇总数据");
    }

    /**
     * 查询库存汇总
     *
     * @param stockInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('stock:record:list')")
    @GetMapping("/locationMatList")
    public AjaxResult locationMatList(StockInfo stockInfo) {
        List<StockInfo> list = stockInfoService.selectStockInfoStatsList(stockInfo);
        if (CollectionUtils.isNotEmpty(list)) {
            for (StockInfo info : list) {
                info.setUnitName(sysDictDataService.selectDictLabel(dictType, info.getUnitCode()));
            }
        }
        return AjaxResult.success(list);
    }

    /**
     * 查询库存信息列表
     *
     * @param stockInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('stock:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(StockInfo stockInfo) {
        startPage();
        List<StockInfo> list = stockInfoService.selectStockInfoList(stockInfo);
        if (CollectionUtils.isNotEmpty(list)) {
            for (StockInfo info : list) {
                info.setWarehouseName(baseWarehouseService.selectBaseWarehouseNameByWarehouseCode(info.getWarehouseCode()));
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出库存信息列表
     *
     * @param response
     * @param stockInfo
     */
    @PreAuthorize("@ss.hasPermi('stock:info:export')")
    @Log(title = "库存信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StockInfo stockInfo) {
        List<StockInfo> list = stockInfoService.selectStockInfoList(stockInfo);
        ExcelUtils<StockInfo> util = new ExcelUtils<StockInfo>(StockInfo.class);
        util.exportExcel(response, list, "库存信息数据");
    }

    /**
     * 获取库存信息详细信息
     *
     * @param infoId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('stock:info:query')")
    @GetMapping(value = "/{infoId}")
    public AjaxResult getInfo(@PathVariable("infoId") Long infoId) {
        return AjaxResult.success(stockInfoService.selectStockInfoByInfoId(infoId));
    }

    /**
     * 新增库存信息
     *
     * @param stockInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('stock:info:add')")
    @Log(title = "库存信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StockInfo stockInfo) {
        return toAjax(stockInfoService.insertStockInfo(stockInfo));
    }

    /**
     * 修改库存信息
     *
     * @param stockInfo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('stock:info:edit')")
    @Log(title = "库存信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StockInfo stockInfo) {
        return toAjax(stockInfoService.updateStockInfo(stockInfo));
    }

    /**
     * 删除库存信息
     *
     * @param infoIds
     * @return
     */
    @PreAuthorize("@ss.hasPermi('stock:info:remove')")
    @Log(title = "库存信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public AjaxResult remove(@PathVariable Long[] infoIds) {
        return toAjax(stockInfoService.deleteStockInfoByInfoIds(infoIds));
    }

    /**
     * 扫码提交上架
     *
     * @param putOnRequestBody
     * @return
     */
    @PostMapping("submitPutOn")
    public AjaxResult submitPutOn(@RequestBody PutOnRequestBody putOnRequestBody) {
        StockMatLabel baseMat = stockMatLabelService.selectStockMatLabelByMatCode(putOnRequestBody.getLocationCode());
        List<MatLabelRequestData> matLabelList = new ArrayList<>();
        if (baseMat != null) {
            MatLabelRequestData m = new MatLabelRequestData();
            m.setMatCode(baseMat.getMatCode());
            m.setMatName(baseMat.getMatName());
            m.setUsableQuantity(baseMat.getUsableQuantity());
            m.setReceivedQuantity(baseMat.getReceivedQuantity());
            matLabelList.add(m);
        }
        putOnRequestBody.setMatLabelList(matLabelList);
        if (putOnRequestBody == null) {
            return error("系统繁忙，请稍后再试！");
        }
        return stockInfoService.submitPutOn(getUsername(), putOnRequestBody);
    }

    /**
     * 扫码提交下架
     *
     * @param putOffRequestBody
     * @return
     */
    @PostMapping("submitPutOff")
    public AjaxResult submitPutOff(@RequestBody PutOffRequestBody putOffRequestBody) {
        StockMatLabel baseMat = stockMatLabelService.selectStockMatLabelByMatCode(putOffRequestBody.getLocationCode());
        List<MatLabelRequestData> matLabelList = new ArrayList<>();
        if (baseMat != null) {
            MatLabelRequestData m = new MatLabelRequestData();
            m.setWarehouseCode(baseMat.getWarehouseCode());
            m.setLocationCode(baseMat.getLocationCode());
            m.setLabelId(baseMat.getLabelId());
            m.setMatCode(baseMat.getMatCode());
            m.setMatName(baseMat.getMatName());
            m.setFdCode(baseMat.getFdCode());
            m.setFigNum(baseMat.getFigNum());
            m.setMatGroup(baseMat.getMatGroup());
            m.setMatClass(baseMat.getMatClass());
            m.setUnitCode(baseMat.getUnitCode());
            m.setBatch(baseMat.getBatch());
            m.setSupplierCode(baseMat.getSupplierCode());
            m.setSupplierName(baseMat.getSupplierName());
            m.setQuantity(baseMat.getQuantity());
            m.setUsableQuantity(baseMat.getUsableQuantity());
            m.setReceivedQuantity(baseMat.getReceivedQuantity());
            m.setUnitPrice(baseMat.getUnitPrice());
            m.setOrderNo(baseMat.getOrderNo());
            m.setOrderType(baseMat.getOrderType());
            matLabelList.add(m);
        }
        putOffRequestBody.setMatLabelList(matLabelList);
        if (putOffRequestBody == null) {
            return error("系统繁忙，请稍后再试！");
        }
        return stockInfoService.submitPutOff(getUsername(), putOffRequestBody);
    }
}
